﻿using UnityEditor;
using UnityEngine;
using System.Diagnostics;
using System.IO;

public class OpenShaderWithRider
{
    private static string riderPath = @"D:\Program Files\JetBrains\JetBrains Rider 2025.2.0.1\bin\rider64.exe";

    [MenuItem("Assets/Open Shader in Rider", true)]
    private static bool Validate()
    {
        return Selection.activeObject != null &&
               AssetDatabase.GetAssetPath(Selection.activeObject).EndsWith(".shader");
    }

    [MenuItem("Assets/Open Shader in Rider")]
    private static void OpenShader()
    {
        string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
        string fullPath = Path.GetFullPath(assetPath);
        
        var startInfo = new ProcessStartInfo
        {
            FileName = riderPath,
            Arguments = $"\"{fullPath}\"",
            CreateNoWindow = true,          // 不创建终端窗口
            UseShellExecute = false,        // 不通过 shell 启动
            RedirectStandardOutput = false, // 不需要重定向输出
            RedirectStandardError = false
        };

        // 打开整个工程并定位到该文件
        Process.Start(startInfo);        
    }
}
